package com.google.code.swingonfire.component.foldable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.SwingConstants;

public class DefaultJFoldableTrigger extends AbstractJFoldableTrigger implements ActionListener, MouseListener
{

    private static final long serialVersionUID = -8682840864449402221L;

    private AbstractJFoldableIndicator indicator;
    private int indicatorLocation = -1;
    private Component triggerComponent;

    public DefaultJFoldableTrigger()
    {
        this(null);
    }

    public DefaultJFoldableTrigger(Component triggerComponent)
    {
        this(new DefaultJFoldableIndicator(), triggerComponent);
    }

    public DefaultJFoldableTrigger(AbstractJFoldableIndicator indicator, Component triggerComponent)
    {
        super();

        setLayout(new BorderLayout());
        setOpaque(true);
        setBackground(Color.RED);

        setIndicator(indicator);
        setTriggerComponent(triggerComponent);

        addMouseListener(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void updateLayout()
    {
        JFoldablePanel panel = getPanel();

        if (panel != null)
        {
            int direction = panel.getDirection();
            int triggerLocation = panel.getTriggerLocation();
            int indicatorLocation =
                ((triggerLocation == SwingConstants.NORTH) || (triggerLocation == SwingConstants.SOUTH))
                    ? SwingConstants.WEST : SwingConstants.NORTH;

            setIndicator(indicator, indicatorLocation);

            if (indicator != null)
            {
                indicator.setDirection(direction);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setPanel(JFoldablePanel panel)
    {
        super.setPanel(panel);

        if ((panel != null) && (indicator != null))
        {
            indicator.setDirection(panel.getDirection());
        }
    }

    public AbstractJFoldableIndicator getIndicator()
    {
        return indicator;
    }

    public void setIndicator(AbstractJFoldableIndicator indicator)
    {
        setIndicator(indicator, indicatorLocation);
    }

    public void setIndicator(AbstractJFoldableIndicator indicator, int indicatorLocation)
    {
        if ((this.indicator != indicator) || (this.indicatorLocation != indicatorLocation))
        {
            if (this.indicator != null)
            {
                this.indicator.removeActionListener(this);
                remove(this.indicator);
            }

            this.indicator = indicator;
            this.indicatorLocation = indicatorLocation;

            if (this.indicator != null)
            {
                switch (indicatorLocation)
                {
                    case SwingConstants.NORTH:
                        add(this.indicator, BorderLayout.NORTH);
                        break;

                    case SwingConstants.SOUTH:
                        add(this.indicator, BorderLayout.SOUTH);
                        break;

                    case SwingConstants.EAST:
                        add(this.indicator, BorderLayout.EAST);
                        break;

                    case SwingConstants.WEST:
                        add(this.indicator, BorderLayout.WEST);
                        break;

                    default:
                        break;
                }

                if (getPanel() != null)
                {
                    indicator.setDirection(getPanel().getDirection());
                }

                this.indicator.addActionListener(this);
            }
        }
    }

    public int getIndicatorLocation()
    {
        return indicatorLocation;
    }

    public Component getTriggerComponent()
    {
        return triggerComponent;
    }

    public void setTriggerComponent(Component triggerComponent)
    {
        if (this.triggerComponent != triggerComponent)
        {
            if (this.triggerComponent != null)
            {
                remove(this.triggerComponent);
            }

            this.triggerComponent = triggerComponent;

            if (this.triggerComponent != null)
            {
                add(this.triggerComponent, BorderLayout.CENTER);
            }
        }
    }

    @Override
    public void foldingInProgress(FoldableEvent event)
    {
        if (indicator != null)
        {
            indicator.foldingInProgress(event);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        fireActionPerformed();
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        // intentionally left blank
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        // intentionally left blank
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        fireActionPerformed();
        e.consume();
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
        // intentionally left blank
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        // intentionally left blank
    }

    @Override
    public void paint(Graphics g)
    {
        // TODO Auto-generated method stub
        super.paint(g);
    }

    
}
